/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.view;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.render.Paint;
import ohos.agp.utils.Rect;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

import com.xuexiang.xuidemo.ResourceTable;

/**
 * 自定义流布局
 *
 * @since 2021-03-24
 */
public class DistributionData extends DirectionalLayout {
    private DirectionalLayout directionalLayout = null;
    private DirectionalLayout thistionalLayout = null;
    private Context context;
    private int padding; // 宽度额外加的 单位都算成vp
    private int layoutwidth;
    private int number; // 总的宽度
    private int dqkj; // 生成当前的控件所需的宽度
    private static final int anInt2 = 2;
    private static final int anInt5 = 5;
    private static final int anInt10 = 10;
    private static final int anInt30 = 30;
    private int ysckj = 0;
    private int laoutid = 1;

    /**
     * 构造方法
     *
     * @param context 上下文
     */
    public DistributionData(Context context) {
        super(context);
        this.context = context;
        thistionalLayout = this;
        this.directionalLayout = createColumnContainer();
        thistionalLayout.addComponent(directionalLayout); // 把根布局加入到最大的布局中
    }

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param attrSet 样式
     */
    public DistributionData(Context context, AttrSet attrSet) {
        super(context, attrSet);
        thistionalLayout = this;
        this.context = context;
        padding = AttrHelper.vp2px(anInt10, context);
        layoutwidth = getDisplayWidthInPx();
        number = layoutwidth - AttrHelper.vp2px(anInt10, context) * anInt2; // 获取整个屏幕的宽度单位为vp
        setLayoutConfig(createRootViewConfig());
        setOrientation(DirectionalLayout.VERTICAL); // 设置最外层的控件为竖直方向
        directionalLayout = createColumnContainer();
        thistionalLayout.addComponent(directionalLayout); // 把根布局加入到最大的布局中
    }

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param attrSet 样式
     * @param styleName 名字
     */
    public DistributionData(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    /**
     * 设置显示的数据
     *
     * @param data
     */
    public void setCompoentText(String data) {
        Paint paint = new Paint();
        Rect rect = paint.getTextBounds(data);

        int with = rect.getWidth(); // 当前控件所需的宽度 with + pading +字体
        dqkj = with + padding * anInt2 + AttrHelper.vp2px(anInt10, context) * anInt2
            + AttrHelper.vp2px(anInt10, context);
        int xl = number - ysckj;
        if (dqkj <= xl) { // 表示还有位置
            ysckj += dqkj;
        } else {
            ysckj = 0;
            this.directionalLayout = createColumnContainer(); // 创建一个布局
            thistionalLayout.addComponent(directionalLayout); // 把根布局加入到最大的布局中
        }
        Text text = new Text(context);
        text.setTextAlignment(TextAlignment.CENTER); // 设置居中
        ComponentContainer.LayoutConfig config = new LayoutConfig(with + padding * anInt2
            + AttrHelper.vp2px(anInt10, context) * anInt2 + AttrHelper.vp2px(anInt10, context),
            AttrHelper.vp2px(anInt30, context));
        config.setMarginLeft(AttrHelper.vp2px(anInt10, context));
        config.setMarginTop(AttrHelper.vp2px(anInt5, context));
        text.setPaddingLeft(padding);
        text.setPaddingRight(padding);
        text.setLayoutConfig(config);
        Element touchedBackGround = ElementScatter.getInstance(context)
            .parse(ResourceTable.Graphic_background_text);
        text.setBackground(touchedBackGround); // 设置背景颜色
        text.setText(data);
        text.setTextSize(AttrHelper.vp2px(anInt10, context));
        text.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
            }
        });
        directionalLayout.addComponent(text);
    }

    /**
     * 创建行的容器,加入到总的布局中
     *
     * @return  DirectionalLayout组件
     */
    private DirectionalLayout createColumnContainer() {
        LayoutConfig config = new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            ComponentContainer.LayoutConfig.MATCH_CONTENT);
        config.setMarginLeft(AttrHelper.vp2px(anInt10, context));
        config.setMarginRight(AttrHelper.vp2px(anInt10, context));
        config.setMarginTop(AttrHelper.vp2px(anInt2, context));
        DirectionalLayout columnContainer = new DirectionalLayout(getContext());
        columnContainer.setId(laoutid);
        columnContainer.setLayoutConfig(config);
        columnContainer.setOrientation(DirectionalLayout.HORIZONTAL);
        this.laoutid++;
        return columnContainer;
    }

    private ComponentContainer.LayoutConfig createRootViewConfig() {
        LayoutConfig config = new LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,
            ComponentContainer.LayoutConfig.MATCH_CONTENT);
        return config;
    }

    /**
     * 获取屏幕宽度
     *
     * @return 屏幕宽度
     */
    private int getDisplayWidthInPx() {
        Display display = DisplayManager.getInstance().getDefaultDisplay(context).get();
        return display.getAttributes().width;
    }

    /**
     * public的参数不能定义
     *
     * @param yck
     * @param lid
     */
    public void setInt(int yck, int lid) {
        this.ysckj = yck;
        this.laoutid = lid;
    }

    /**
     * 获取到lauid
     *
     * @return int类型
     */
    public int getLaoutid() {
        return this.laoutid;
    }
}
